
// inventory.js - 库存指标页面专用JavaScript功能

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    initInventoryPage();
});

// 库存指标页面初始化函数
function initInventoryPage() {
    initInventoryCharts();
    bindInventoryEvents();
}

// 库存图表初始化
function initInventoryCharts() {
    console.log('初始化库存指标图表');

    // 初始化所有图表
    initLeft1Chart();
    initLeft2Chart();
    initLeft3Chart();
    initLeft4Chart();
    initRight1Chart();
    initRight2Chart();
    initRight3Chart();
    initRight4Chart();

    // 监听窗口大小变化
    window.addEventListener('resize', function() {
        resizeInventoryCharts();
    });
}

// 初始化图表通用函数
function initChart(chartClass, getOptionFunction, chartName) {
    const chartDom = document.querySelector(`#inventory .${chartClass}`);
    if (!chartDom) {
        console.log(`未找到 ${chartName} 图表容器: ${chartClass}`);
        return null;
    }

    if (chartDom.offsetWidth === 0 || chartDom.offsetHeight === 0) {
        console.log(`${chartName} 图表容器尺寸为0，延迟初始化`);
        setTimeout(() => initChart(chartClass, getOptionFunction, chartName), 100);
        return null;
    }

    try {
        let chartInstance = echarts.getInstanceByDom(chartDom);
        if (chartInstance) {
            chartInstance.dispose();
        }

        chartInstance = echarts.init(chartDom);
        const option = getOptionFunction();
        chartInstance.setOption(option);

        console.log(`${chartName} 图表初始化成功`);

        setTimeout(() => {
            chartInstance.resize();
        }, 50);

        return chartInstance;

    } catch (error) {
        console.error(`初始化 ${chartName} 图表失败:`, error);
        return null;
    }
}

// 左列第一个图表 - 近三年库存数据
function initLeft1Chart() {
    initChart('inventory-chart-left1', getLeft1Option, '近三年库存数据');
}

// 左列第二个图表 - 各行业连续12个月库存数据
function initLeft2Chart() {
    initChart('inventory-chart-left2', getLeft2Option, '各行业连续12个月库存数据');
}

// 左列第三个图表 - 连续12个月库存数据
function initLeft3Chart() {
    initChart('inventory-chart-left3', getLeft3Option, '连续12个月库存数据');
}

// 左列第四个图表 - 2025年库存数据
function initLeft4Chart() {
    initChart('inventory-chart-left4', getLeft4Option, '2025年库存数据');
}

// 右列第一个图表 - 近三年库存周转率
function initRight1Chart() {
    initChart('inventory-chart-right1', getRight1Option, '近三年库存周转率');
}

// 右列第二个图表 - 连续12个月库存周转率
function initRight2Chart() {
    initChart('inventory-chart-right2', getRight2Option, '连续12个月库存周转率');
}

// 右列第三个图表 - 2025年库存周转率
function initRight3Chart() {
    initChart('inventory-chart-right3', getRight3Option, '2025年库存周转率');
}

// 右列第四个图表 - 近三年闲废物资统计
function initRight4Chart() {
    initChart('inventory-chart-right4', getRight4Option, '近三年闲废物资统计');
}

// 获取左1图表配置 - 近三年库存数据
function getLeft1Option() {
    var color = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];

    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            data: ['2023年', '2024年', '2025年'],
            right: 6,
            top: 0,
            textStyle: {
                color: '#4c9bfd'
            }
        },
        grid: {
            top: '22%',
            left: '2%',
            right: '6%',
            bottom: '2%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: ['水电', '风电', '光伏', '核电', '煤炭'],
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            name: '库存金额(万元)',
            nameTextStyle: {
                color: 'rgba(255, 255, 255, 0.6)',
                fontSize: 12
            },
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(255, 255, 255, 0.1)'
                }
            }
        },
        series: [
            {
                name: '2023年',
                type: 'bar',
                data: [32000, 28000, 15000, 45000, 38000],
                itemStyle: {
                    color: color[0]
                }
            },
            {
                name: '2024年',
                type: 'bar',
                data: [35000, 32000, 18000, 48000, 42000],
                itemStyle: {
                    color: color[1]
                }
            },
            {
                name: '2025年',
                type: 'bar',
                data: [38000, 35000, 20000, 52000, 45000],
                itemStyle: {
                    color: color[2]
                }
            }
        ]
    };
}

// 获取左2图表配置 - 各行业连续12个月库存数据
function getLeft2Option() {
    var color = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];

    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            data: ['水电', '风电', '光伏', '核电', '煤炭'],
            right: 6,
            top: 0,
            textStyle: {
                color: '#4c9bfd'
            }
        },
        grid: {
            top: '22%',
            left: '2%',
            right: '6%',
            bottom: '2%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            name: '库存金额(万元)',
            nameTextStyle: {
                color: 'rgba(255, 255, 255, 0.6)',
                fontSize: 12
            },
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(255, 255, 255, 0.1)'
                }
            }
        },
        series: [
            {
                name: '水电',
                type: 'line',
                data: [42000, 43000, 41000, 44000, 45000, 46000, 47000, 48000, 49000, 50000, 51000, 52000],
                itemStyle: {
                    color: color[0]
                },
                symbol: "circle",
                symbolSize: 6
            },
            {
                name: '风电',
                type: 'line',
                data: [35000, 36000, 37000, 38000, 39000, 40000, 41000, 42000, 43000, 44000, 45000, 46000],
                itemStyle: {
                    color: color[1]
                },
                symbol: "circle",
                symbolSize: 6
            },
            {
                name: '光伏',
                type: 'line',
                data: [18000, 19000, 20000, 21000, 22000, 23000, 24000, 25000, 26000, 27000, 28000, 29000],
                itemStyle: {
                    color: color[2]
                },
                symbol: "circle",
                symbolSize: 6
            },
            {
                name: '核电',
                type: 'line',
                data: [48000, 49000, 50000, 51000, 52000, 53000, 54000, 55000, 56000, 57000, 58000, 59000],
                itemStyle: {
                    color: color[3]
                },
                symbol: "circle",
                symbolSize: 6
            },
            {
                name: '煤炭',
                type: 'line',
                data: [42000, 43000, 44000, 45000, 46000, 47000, 48000, 49000, 50000, 51000, 52000, 53000],
                itemStyle: {
                    color: color[4]
                },
                symbol: "circle",
                symbolSize: 6
            }
        ]
    };
}

// 获取左3图表配置 - 连续12个月库存数据 (原来的TwelveMonthInventory)
function getLeft3Option() {
    var color = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];

    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            }
        },
        grid: {
            top: '22%',
            left: '2%',
            right: '6%',
            bottom: '2%',
            show: true,
            borderColor: '#012f4a',
            containLabel: true
        },
        legend: {
            data: ['水电', '风电', '光伏', '核电'],
            right: 6,
            top: 0,
            textStyle: {
                color: '#4c9bfd'
            }
        },
        xAxis: {
            type: 'category',
            data: ['25年4月', '25年5月', '25年6月', '25年7月', '25年8月', '25年9月'],
            axisTick: {
                show: false
            },
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            axisLine: {
                show: false
            },
            boundaryGap: false
        },
        yAxis: {
            type: 'value',
            name: '库存金额(万元)',
            nameTextStyle: {
                fontSize: 12,
                color: "rgba(255, 255, 255, 0.6)",
                fontWeight: 'bold'
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            axisLine: {
                show: false
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(255, 255, 255, 0.1)'
                }
            },
            boundaryGap: false
        },
        series: [
            {
                name: '水电',
                type: 'line',
                stack: 'Total',
                data: [4397, 4642, 4733, 5038, 5831, 5776],
                color: color[0],
                symbol: "circle",
                symbolSize: 6,
                itemStyle: {
                    color: color[0],
                    borderColor: "rgba(221, 220, 107, .1)",
                    borderWidth: 8
                },
            },
            {
                name: '风电',
                type: 'line',
                stack: 'Total',
                data: [13540, 13773, 14731, 14170, 13545, 13527],
                color: color[1],
                symbol: "circle",
                symbolSize: 6,
                itemStyle: {
                    color: color[1],
                    borderColor: "rgba(221, 220, 107, .1)",
                    borderWidth: 8
                },
            },
            {
                name: '光伏',
                type: 'line',
                stack: 'Total',
                data: [1214, 1273, 1286, 1356, 1384, 1495],
                color: color[2],
                symbol: "circle",
                symbolSize: 6,
                itemStyle: {
                    color: color[2],
                    borderColor: "rgba(221, 220, 107, .1)",
                    borderWidth: 8
                },
            },
            {
                name: '核电',
                type: 'line',
                stack: 'Total',
                data: [27127, 14332, 14524, 14740, 15071, 15408],
                color: color[3],
                symbol: "circle",
                symbolSize: 6,
                itemStyle: {
                    color: color[3],
                    borderColor: "rgba(221, 220, 107, .1)",
                    borderWidth: 8
                },
            }
        ]
    };
}

// 获取左4图表配置 - 2025年库存数据
function getLeft4Option() {
    var color = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];

    return {
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            right: 10,
            top: 'center',
            textStyle: {
                color: '#4c9bfd'
            }
        },
        series: [
            {
                name: '库存分布',
                type: 'pie',
                radius: ['40%', '70%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: '18',
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: [
                    { value: 38000, name: '水电', itemStyle: { color: color[0] } },
                    { value: 35000, name: '风电', itemStyle: { color: color[1] } },
                    { value: 20000, name: '光伏', itemStyle: { color: color[2] } },
                    { value: 52000, name: '核电', itemStyle: { color: color[3] } },
                    { value: 45000, name: '煤炭', itemStyle: { color: color[4] } }
                ]
            }
        ]
    };
}

// 获取右1图表配置 - 近三年库存周转率
function getRight1Option() {
    var color = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];

    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            data: ['2023年', '2024年', '2025年'],
            right: 6,
            top: 0,
            textStyle: {
                color: '#4c9bfd'
            }
        },
        grid: {
            top: '22%',
            left: '2%',
            right: '6%',
            bottom: '2%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: ['水电', '风电', '光伏', '核电', '煤炭'],
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            name: '周转率(次)',
            nameTextStyle: {
                color: 'rgba(255, 255, 255, 0.6)',
                fontSize: 12
            },
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(255, 255, 255, 0.1)'
                }
            }
        },
        series: [
            {
                name: '2023年',
                type: 'bar',
                data: [4.2, 5.1, 6.3, 3.8, 8.2],
                itemStyle: {
                    color: color[0]
                }
            },
            {
                name: '2024年',
                type: 'bar',
                data: [4.5, 5.4, 6.8, 4.1, 8.5],
                itemStyle: {
                    color: color[1]
                }
            },
            {
                name: '2025年',
                type: 'bar',
                data: [4.8, 5.7, 7.2, 4.5, 8.9],
                itemStyle: {
                    color: color[2]
                }
            }
        ]
    };
}

// 获取右2图表配置 - 连续12个月库存周转率
function getRight2Option() {
    var color = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];

    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            data: ['水电', '风电', '光伏', '核电', '煤炭'],
            right: 6,
            top: 0,
            textStyle: {
                color: '#4c9bfd'
            }
        },
        grid: {
            top: '22%',
            left: '2%',
            right: '6%',
            bottom: '2%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            name: '周转率(次)',
            nameTextStyle: {
                color: 'rgba(255, 255, 255, 0.6)',
                fontSize: 12
            },
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(255, 255, 255, 0.1)'
                }
            }
        },
        series: [
            {
                name: '水电',
                type: 'line',
                data: [4.5, 4.6, 4.7, 4.8, 4.9, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6],
                itemStyle: {
                    color: color[0]
                },
                symbol: "circle",
                symbolSize: 6
            },
            {
                name: '风电',
                type: 'line',
                data: [5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6.0, 6.1, 6.2, 6.3],
                itemStyle: {
                    color: color[1]
                },
                symbol: "circle",
                symbolSize: 6
            },
            {
                name: '光伏',
                type: 'line',
                data: [6.5, 6.6, 6.7, 6.8, 6.9, 7.0, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6],
                itemStyle: {
                    color: color[2]
                },
                symbol: "circle",
                symbolSize: 6
            },
            {
                name: '核电',
                type: 'line',
                data: [4.0, 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5.0, 5.1],
                itemStyle: {
                    color: color[3]
                },
                symbol: "circle",
                symbolSize: 6
            },
            {
                name: '煤炭',
                type: 'line',
                data: [8.0, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9.0, 9.1],
                itemStyle: {
                    color: color[4]
                },
                symbol: "circle",
                symbolSize: 6
            }
        ]
    };
}

// 获取右3图表配置 - 2025年库存周转率
function getRight3Option() {
    var color = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];

    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            top: '22%',
            left: '2%',
            right: '6%',
            bottom: '2%',
            containLabel: true
        },
        xAxis: {
            type: 'value',
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(255, 255, 255, 0.1)'
                }
            }
        },
        yAxis: {
            type: 'category',
            data: ['煤炭', '光伏', '风电', '水电', '核电'],
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            }
        },
        series: [
            {
                name: '周转率',
                type: 'bar',
                data: [
                    { value: 8.9, itemStyle: { color: color[4] } },
                    { value: 7.2, itemStyle: { color: color[2] } },
                    { value: 5.7, itemStyle: { color: color[1] } },
                    { value: 4.8, itemStyle: { color: color[0] } },
                    { value: 4.5, itemStyle: { color: color[3] } }
                ],
                label: {
                    show: true,
                    position: 'right',
                    color: '#fff'
                }
            }
        ]
    };
}

// 获取右4图表配置 - 近三年闲废物资统计
function getRight4Option() {
    var color = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];

    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            data: ['闲置物资', '废旧物资'],
            right: 6,
            top: 0,
            textStyle: {
                color: '#4c9bfd'
            }
        },
        grid: {
            top: '22%',
            left: '2%',
            right: '6%',
            bottom: '2%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: ['2023年', '2024年', '2025年'],
            axisLabel: {
                color: 'rgba(255, 255, 255, 0.6)'
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            }
        },
        yAxis: [
            {
                type: 'value',
                name: '金额(万元)',
                nameTextStyle: {
                    color: 'rgba(255, 255, 255, 0.6)',
                    fontSize: 12
                },
                axisLabel: {
                    color: 'rgba(255, 255, 255, 0.6)'
                },
                splitLine: {
                    lineStyle: {
                        color: 'rgba(255, 255, 255, 0.1)'
                    }
                }
            },
            {
                type: 'value',
                name: '处置率(%)',
                nameTextStyle: {
                    color: 'rgba(255, 255, 255, 0.6)',
                    fontSize: 12
                },
                axisLabel: {
                    color: 'rgba(255, 255, 255, 0.6)',
                    formatter: '{value}%'
                },
                splitLine: {
                    show: false
                },
                max: 100
            }
        ],
        series: [
            {
                name: '闲置物资',
                type: 'bar',
                data: [1250, 980, 680],
                itemStyle: {
                    color: color[0]
                }
            },
            {
                name: '废旧物资',
                type: 'bar',
                data: [890, 1120, 950],
                itemStyle: {
                    color: color[1]
                }
            },
            {
                name: '处置率',
                type: 'line',
                yAxisIndex: 1,
                data: [65, 78, 85],
                itemStyle: {
                    color: color[2]
                },
                symbol: "circle",
                symbolSize: 6
            }
        ]
    };
}

// 图表尺寸调整
function resizeInventoryCharts() {
    const charts = document.querySelectorAll('#inventory [class*="inventory-chart-left"], #inventory [class*="inventory-chart-right"], #inventory .inventory-chart-placeholder');
    charts.forEach(chartDom => {
        const chartInstance = echarts.getInstanceByDom(chartDom);
        if (chartInstance) {
            setTimeout(() => {
                chartInstance.resize();
            }, 50);
        }
    });
}

// 绑定库存页面事件
function bindInventoryEvents() {
    // 这里可以添加库存页面的特定事件绑定
    console.log('绑定库存页面事件');
}

// 导出页面切换时需要调用的函数
function initInventoryChartsOnShow() {
    // 延迟初始化以确保DOM完全渲染
    setTimeout(() => {
        // initInventoryCharts();
        resizeInventoryCharts();
    }, 150);
}

// 清理库存页面资源
function cleanupInventoryPage() {
    const charts = document.querySelectorAll('#inventory [class*="inventory-chart-left"], #inventory [class*="inventory-chart-right"], #inventory .inventory-chart-placeholder');
    charts.forEach(chartDom => {
        const chartInstance = echarts.getInstanceByDom(chartDom);
        if (chartInstance) {
            chartInstance.dispose();
        }
    });
    console.log('库存页面资源清理完成');
}


